<?php
class RemoteFile {
	private $url;
	public $filename;

	function RemoteFile($url) {
		$this->url = $url;
		$this->filename = $this->get($url);
	}

	/**
	 * Downloads a url to a local file using the SimplePie File Class
	 *
	 * @param string $url the URL of the file to download
	 * @return mixed WP_Error on failure, string Filename on success.
	 */
	private function get( $url ) {
		//WARNING: The file is not automatically deleted, The script must unlink() the file.
		if( ! $url )
			return ErrorHandler::instance()->add('http_no_url', __('Invalid URL Provided'));

		$tmpfname = FileSystemTools::make_temporary_file($url);
		if( ! $tmpfname )
			return ErrorHandler::instance()->add('http_no_file', __('Could not create Temporary file'));

		var_dump(array('l26' => $tmpfname));
		$handle = @fopen($tmpfname, 'w');
		if( ! $handle )
			return ErrorHandler::instance()->add('http_no_file', __('Could not create Temporary file'));

		require_once( MP_PATH . '/mp-includes/simplepie.inc' );
		$request = new SimplePie_File($url);

		if( !$request->success ){
			fclose($handle);
			unlink($tmpfname);
			return ErrorHandler::instance()->add('http_404', trim($request->error));
		}
		fwrite($handle, $request->body);
		fclose($handle);

		return $tmpfname;
	}

}